package com.carltechnology.mahjonggamecaculation.dao;

import com.carltechnology.mahjonggamecaculation.base.MyAbstractSubscriber;
import com.carltechnology.mahjonggamecaculation.dao.impl.MahjongGameDataSourceComponentComponent;
import com.carltechnology.mahjonggamecaculation.mode.IDetailGameData;
import com.carltechnology.mahjonggamecaculation.mode.IGameData;
import com.carltechnology.mahjonggamecaculation.mode.IGameRuntimeData;
import com.carltechnology.mahjonggamecaculation.utils.Utils;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by WZTCM on 2016/6/28.
 */
public class MahjongDataSourceHelper {
    private IMahjongGameDataSourceComponent dataSource;

    public MahjongDataSourceHelper(IMahjongDataDao iDao) {
        dataSource = new MahjongGameDataSourceComponentComponent(iDao);
    }

    public void getMahjongInfo(MyAbstractSubscriber<IGameData> subscriber) {
        getMahjongDataObservable().subscribe(subscriber);
    }

    public void getMahjongDetailInfo(final int id,MyAbstractSubscriber<IDetailGameData> subscriber) {
        getMahjongDetailDataObservable(id).subscribe(subscriber);
    }

    public void getMahjongRuntimeInfo(MyAbstractSubscriber<IGameRuntimeData> subscriber) {
        getRuntimeDataObservable().subscribe(subscriber);
    }

    private Observable<IGameData> getMahjongDataObservable() {
        return Observable.create(new Observable.OnSubscribe<IGameData>() {
            @Override
            public void call(Subscriber<? super IGameData> subscriber) {
                dataSource.getMahjongGameData(subscriber);
            }
        }).onBackpressureBuffer(Utils.CONSTANT.ONBACKBUFFER_SIZE.getConstant())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }


    private Observable<IDetailGameData> getMahjongDetailDataObservable(final int id) {
        return Observable.create(new Observable.OnSubscribe<IDetailGameData>() {
            @Override
            public void call(Subscriber<? super IDetailGameData> subscriber) {
                dataSource.getMahjongDetailData(id,subscriber);
            }
        }).onBackpressureBuffer(Utils.CONSTANT.ONBACKBUFFER_SIZE.getConstant())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }


    private Observable<IGameRuntimeData> getRuntimeDataObservable() {
        return Observable.create(new Observable.OnSubscribe<IGameRuntimeData>() {
            @Override
            public void call(Subscriber<? super IGameRuntimeData> subscriber) {
                dataSource.getMahjongRuntimeData(subscriber);
            }
        }).onBackpressureBuffer(Utils.CONSTANT.ONBACKBUFFER_SIZE.getConstant())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
}
